﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using ERMap;

namespace PhoneBook.Group
{
    public class PbMember
    {
        public static DataSet list(string phonebookid)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;

            return ERMapHelper.getdata("b_pbmember", keys);
        }

        public static void reset(string phonebookid, string righttype, IList memberids)
        {
            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;
            keys["righttype"] = righttype;
            ERMapHelper.destroy("b_pbmember", keys);

            foreach (var item in memberids)
            {
                Hashtable pbright = new Hashtable();
                pbright["phonebookid"] = phonebookid;
                pbright["righttype"] = righttype;
                pbright["memberid"] = item.ToString();

                ERMapHelper.setmodel("b_pbmember", "phonebookid,righttype,memberid", pbright);
            }
        }

        public static DataSet querysharemember(string phonebookid)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" select e.*,d.departmentname ");
            sb.Append(" from b_pbmember m inner join b_department d on m.memberid = d.departmentid and m.righttype = 0 ");
            sb.Append(" inner join b_employee e on e.departmentid = d.departmentid ");
            sb.Append(" where m.phonebookid = @phonebookid ");
            sb.Append(" and isnull(m.isdel,0)=0 and isnull(d.isdel,0)=0 ");
            sb.Append(" and isnull(e.isdel,0)=0 ");

            sb.Append(" union ");

            sb.Append(" select e.*,d.departmentname ");
            sb.Append(" from b_pbmember m inner join b_employee e on m.memberid = e.employeeid and m.righttype = 1 ");
            sb.Append(" inner join b_department d on e.departmentid = d.departmentid ");
            sb.Append(" where m.phonebookid = @phonebookid ");
            sb.Append(" and isnull(m.isdel,0)=0 and isnull(e.isdel,0)=0 ");
            sb.Append(" and isnull(d.isdel,0)=0 ");

            Hashtable keys = new Hashtable();
            keys["phonebookid"] = phonebookid;

            return ERMapHelper.getCustomData(sb.ToString(), keys);
        }
    }
}
